package com.wxmit.module.system.web;

import com.wxmit.base.domain.ApiResult;
import com.wxmit.framework.satoken.core.annotation.Anonymous;
import com.wxmit.framework.satoken.core.util.StpUserUtil;
import com.wxmit.module.system.domain.bo.LoginBody;
import com.wxmit.module.system.domain.vo.LoginTokenVo;
import com.wxmit.module.system.service.SysLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *  登陆
 * @Author huangxiong
 * @Date 2022/7/11 10:01
 * @Version V1.0
 */
@RestController
@RequestMapping("")
@Tag(name="登录模块")
public class LoginController {

    @Autowired
    private SysLoginService loginService;


    /**
     *  登录方法
     * @param loginBody 登录信息
     * @return 成功则返回登录对象，失败则响应异常
     */
    @PostMapping("/login")
    @Anonymous
    @Operation(summary = "login")
    public ApiResult login(@Validated @RequestBody LoginBody loginBody){
        return ApiResult.successWithData(loginService.login(loginBody));
    }

    @Anonymous
    @PostMapping("/refreshToken")
    @Operation(summary = "loginByRefreshToken")
    public ApiResult<LoginTokenVo> loginByRefreshToken(@RequestBody LoginTokenVo tokenVo){
        return ApiResult.successWithData(loginService.loginByRefreshToken(tokenVo.getRefreshToken()));
    }

    /**
     *  退出方法
     * @return 结果结果
     */
    @PutMapping("/logout")
    @Operation(summary = "logout",description = "退出当前登录用户")
    public ApiResult logout(){
        StpUserUtil.logout();
        return ApiResult.success();
    }


}
